package com.my.yc.d0110;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "LoginServlet", value = "/login.s")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        final String name = request.getParameter("name");
        final String pwd = request.getParameter("pwd");
        if ("yc".equals(name) && "123".equals(pwd)){
            //会话对象，响应重定向可以用会话属性传递数据
            final HttpSession session = request.getSession();
            session.setAttribute("loginedUser",name);
            response.sendRedirect("home.jsp");
        }else {
            // Attribute 请求属性，请求穿法可以用属性传递数据
            request.setAttribute("msg","用户名或密码错误");
            request.getRequestDispatcher("login.jsp").forward(request,response);
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
